package com.example.databasepj.controller;

import com.example.databasepj.Request.*;
import com.example.databasepj.dto.*;
import com.example.databasepj.dtoAdvanced.UserStarAnalysisRequest;
import com.example.databasepj.dtoAdvanced.UserStarAnalysisResponse;
import com.example.databasepj.entity.UserEntity;
import com.example.databasepj.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//控制端口
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private DishService dishService;

    @RequestMapping("/getAll")
    public List<UserEntity> getAll(){
        return userService.getAll();
    }

    @GetMapping("/merchantinfo") //用户搜索商家
    @ResponseBody
    public ExistResponse getMerchantInfo(@RequestParam("query") String merchantName){
        ExistResponse response = merchantService.userSearchMerchant(merchantName);
        return response;
    }

    @GetMapping("/MerchantDetail/{merchant_ID}") //用户查看商家详细信息
    @ResponseBody
    public MerchantInfo getMerchantDetail(@PathVariable int merchant_ID){
        MerchantInfo merchantInfo = merchantService.getMerchantDetail(merchant_ID);
        return merchantInfo;
    }
    @GetMapping("/MerchantBrief/{merchant_ID}") //用户查看商家简略信息
    @ResponseBody
    public MerchantBrief getMerchantBrief(@PathVariable int merchant_ID){
        MerchantBrief merchantBrief = merchantService.getMerchantBrief(merchant_ID);
        return merchantBrief;
    }
    @GetMapping("/UserGetMenu/{merchant_ID}") //用户查看一个商家的菜单
    @ResponseBody
    public List<DishMenuData> getMerchantMenu(@PathVariable int merchant_ID){
        List<DishMenuData> merchantMenuData=merchantService.getMerchantMenu(merchant_ID);
        return merchantMenuData;
    }

    @GetMapping ("/UserGetMerchantRating/{merchant_ID}") //用户查看商户评价
    @ResponseBody
    public List<MerchantReviewResponse> getMerchantRating(@PathVariable int merchant_ID){
        return merchantService.GetMerchantReview(merchant_ID);
    }

    @PostMapping ("/placeOrder/{merchant_ID}") //用户下单
    @ResponseBody
    public void getMerchantRating(@PathVariable int merchant_ID,
                                  @RequestBody OrderRequest orderRequest){
        orderService.insertOrderAndDetail(orderRequest);
    }



    @PostMapping("/UserStarMerchant")//收藏商户
    @ResponseBody
    public NotExistResponse userStarMerchant(@RequestBody UserStarMerchantRequest userStarMerchantRequest) {
        return userService.userStarMerchant(userStarMerchantRequest.getUser_ID(),userStarMerchantRequest.getMerchant_ID());
    }
    @PostMapping("/CancelStarMerchant")//取消收藏商户
    @ResponseBody
    public NotExistResponse userCancelStarMerchant(@RequestBody UserStarMerchantRequest userStarMerchantRequest) {
        return userService.userCancelStarMerchant(userStarMerchantRequest.getUser_ID(),userStarMerchantRequest.getMerchant_ID());
    }

    @PostMapping("/StarDish")//收藏菜品
    @ResponseBody
    public NotExistResponse userStarDish(@RequestBody UserStarDishRequest userStarDishRequest) {
        return userService.userStarDish(userStarDishRequest.getUser_ID(),userStarDishRequest.getDish_ID());
    }

    @PostMapping("/CancelStarDish")//收藏菜品
    @ResponseBody
    public NotExistResponse userCancelStarDish(@RequestBody UserStarDishRequest userStarDishRequest) {
        return userService.userCancelStarDish(userStarDishRequest.getUser_ID(),userStarDishRequest.getDish_ID());
    }



    @PostMapping("/Reserve")//预约餐厅
    @ResponseBody
    public void userReserve(@RequestBody ReserveRequest reserveRequest) {
        messageService.insertReserveMessage(reserveRequest);
    }

    @GetMapping("/UserGetOrderlist") //用户查看所有订单信息
    @ResponseBody
    public  List<OrderDetail> getOrderlist(@RequestParam("user_ID") int user_id){
        return orderService.getUserOrderList(user_id);
    }

    @GetMapping ("/getUserInfo/{userId}") //用户查看用户信息
    @ResponseBody
    public UserData getUserInfo(@PathVariable int userId){
        return userService.getUserInfo(userId);
        //return merchantService.GetMerchantReview(userId);
    }
    @PutMapping("/updateUserInfo/{userId}")//用户更新信息
    public void updateUserInfo(@PathVariable int userId, @RequestBody UserData userInfoRequest) {
        userService.updateUserInfo(userInfoRequest);
    }

    @PostMapping("/merchantReview")//用户评价一次订单中的商户
    @ResponseBody
    public UserReviewMerchantResponse merchantReview(@RequestBody UserReviewMerchantRequest request) {
        return merchantService.insertMerchantReview(request);
    }

    @PostMapping("/dishReview")//用户评价一次订单中的某个菜品
    @ResponseBody
    public void dishReview(@RequestBody UserReviewDishRequest request) {
        dishService.insertDishReview(request);
    }

    @GetMapping("/messages")
    public ResponseEntity<?> getMessagesByUser(@RequestParam("user_ID") Integer userID) {
        if (userID == null) {
            // 如果没有提供user_ID参数，返回400错误响应
            return ResponseEntity.badRequest().body("{\"error\": \"Missing required parameter: user_ID\"}");
        }

        // 模拟从数据库获取消息数据
        List<MessageData> messages = messageService.getMessageList(userID);

        // 返回200 OK响应，消息列表作为响应体
        return ResponseEntity.ok(messages);
    }
    @PostMapping("/starDishAnalysis")//用户收藏菜品分析
    @ResponseBody
    public List<UserStarAnalysisResponse> userStarDishAnalysis(@RequestBody UserStarAnalysisRequest request) {
        return userService.userStarAnalysis(request);
    }





}

